//
//  HomePageDetailVideoItemCell.m
//  ShowYork
//
//  Created by hxh on 2020/10/8.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "HomePageDetailVideoItemCell.h"
#import "HomePageDetailFooterView.h"
#import "HomePageContentModel.h"
@interface HomePageDetailVideoItemCell ()<HomePageDetailFooterViewDelegate>
@property (nonatomic,strong)HomePageItemModel *currentModel;
@property (nonatomic,strong)UIImageView *coverImgView;
@property (nonatomic,strong)HomePageDetailFooterView *footerView;
@property (nonatomic,strong)UIView *longPressView;
@end

@implementation HomePageDetailVideoItemCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
   
    self.backgroundColor = [UIColor blackColor];
   
    self.coverImgView = [[UIImageView alloc]init];
    self.coverImgView.tag = 100;
    self.coverImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.coverImgView];
   
    [self.coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
    __weak typeof(self)weakSelf = self;
    self.footerView = [[HomePageDetailFooterView alloc]initWithCompleteBlock:^(XHFooterType type) {
        if (type == XHFOOTERLIKE) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(videoItemLikeWithCurrentModel:)]) {
                [weakSelf.delegate videoItemLikeWithCurrentModel:weakSelf.currentModel];
            }
        }
        if (type == XHFOOTERDISS) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(videoItemDissWithCurrentModel:)]) {
                [weakSelf.delegate videoItemDissWithCurrentModel:weakSelf.currentModel];
            }
        }
        if (type == XHFOOTERFOLLOW) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(videoItemFollowWithCurrentModel:)]) {
                [weakSelf.delegate videoItemFollowWithCurrentModel:weakSelf.currentModel];
            }
        }
        if (type == XHFOOTERYUSER) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(videoItemUserInfoWithCurrentModel:)]) {
                [weakSelf.delegate videoItemUserInfoWithCurrentModel:weakSelf.currentModel];
            }
        }
        if (type == XHFOOTERCOMMENT) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(videoItemCommentWithCurrentModel:)]) {
                [weakSelf.delegate videoItemCommentWithCurrentModel:weakSelf.currentModel];
            }
        }
        if (type == XHFOOTERTOPIC) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(videoItemTopicListWithCurrentModel:)]) {
                [weakSelf.delegate videoItemTopicListWithCurrentModel:weakSelf.currentModel.tag];
            }
        }
    }];
    
    [self.footerView showInView:self.contentView delegate:self];
    
   
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longP:)];
    [self.coverImgView addGestureRecognizer:longPress];

}

- (void)longP:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(videoItemLongPressToolView)]) {
            [self.delegate videoItemLongPressToolView];
        }
    }
}

- (void)setItemModel:(HomePageItemModel *)itemModel indexPath:(NSIndexPath *)indexPath{
 
    self.currentModel = itemModel;
    
    __block HomePageContentModel *imageModel = nil;
    [itemModel.contents enumerateObjectsUsingBlock:^(HomePageContentModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.seq.intValue == 1) {
            imageModel = obj;
            *stop = YES;
        }
    }];
    

    [self.coverImgView sd_setImageWithURL:[NSURL URLWithString:imageModel.postUrl]];
    
    [self.footerView setItemModel:itemModel animate:NO];
    
}
+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"HomePageDetailVideoItemCell";
    HomePageDetailVideoItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[HomePageDetailVideoItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)hideFooterView{
    [self.footerView hide];
}
- (void)showFooterView{
    [self.footerView show];
}
- (void)updateVideoProgressValue:(CGFloat)value{
    if (self.delegate && [self.delegate respondsToSelector:@selector(videoItemUpdateProgressValue:)]) {
        [self.delegate videoItemUpdateProgressValue:value];
    }
}
- (void)updateProgressValue:(CGFloat)progress{
    self.footerView.progressValue = progress;
}

- (void)resetProgressValue{
    [self.footerView resetProgress];
}
- (void)scrollViewEnable:(BOOL)isEnable{
    if (self.delegate && [self.delegate respondsToSelector:@selector(videoItemScrollViewEnabale:)]) {
        [self.delegate videoItemScrollViewEnabale:isEnable];
    }
}
- (void)progressBeginDraging{
    if (self.delegate && [self.delegate respondsToSelector:@selector(videoItemBeginDraging)]) {
        [self.delegate videoItemBeginDraging];
    }
}
- (void)changeVideoProgressValue:(CGFloat)value{
    if (self.delegate && [self.delegate respondsToSelector:@selector(videoItemChangeProgressValue:)]) {
        [self.delegate videoItemChangeProgressValue:value];
    }
}

@end
